{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set 查询用时:  0.12500977516174316\n",
      "dict 查询用时:  0.12282204627990723\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import faker\n",
    "\n",
    "s = set()\n",
    "d = dict({})\n",
    "f = faker.Faker()\n",
    "for i in range(100000):\n",
    "    name = f.name()\n",
    "    s.add(name)\n",
    "    d[name] = 0\n",
    "\n",
    "start = time.time()       # dict 和 set 查询效率差不多\n",
    "for i in range(1000):\n",
    "    f.name() in s\n",
    "print('set 查询用时: ', time.time() - start)\n",
    "\n",
    "start = time.time()\n",
    "for i in range(1000):\n",
    "    f.name() in d\n",
    "print('dict 查询用时: ', time.time() - start)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set 查询用时:  0.00023484230041503906\n",
      "dict 查询用时:  0.0001862049102783203\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import random\n",
    "\n",
    "s = set()\n",
    "d = dict({})\n",
    "value = None\n",
    "for i in range(10000000):\n",
    "    value = random.random()\n",
    "    s.add(value)\n",
    "    d[value] = 0\n",
    "\n",
    "start = time.time()\n",
    "for i in range(1000):\n",
    "    value in s\n",
    "print('set 查询用时: ', time.time() - start)\n",
    "start = time.time()\n",
    "for i in range(1000):\n",
    "    value in d\n",
    "print('dict 查询用时: ', time.time() - start)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set:  {<__main__.A object at 0x1138c5850>, <__main__.A object at 0x1138c5970>, 1, (1+2j), 'hahh', (1, 2, 3), b'sdf'}\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m/Users/apple/dsc/dsc/25.dict_set/dict_set.ipynb Cell 3\u001b[0m in \u001b[0;36m<cell line: 26>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/25.dict_set/dict_set.ipynb#W2sZmlsZQ%3D%3D?line=21'>22</a>\u001b[0m s\u001b[39m.\u001b[39madd(b)\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/25.dict_set/dict_set.ipynb#W2sZmlsZQ%3D%3D?line=23'>24</a>\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m'\u001b[39m\u001b[39mset: \u001b[39m\u001b[39m'\u001b[39m, s)\n\u001b[0;32m---> <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/25.dict_set/dict_set.ipynb#W2sZmlsZQ%3D%3D?line=25'>26</a>\u001b[0m s\u001b[39m.\u001b[39;49madd([\u001b[39m1\u001b[39;49m,\u001b[39m2\u001b[39;49m,\u001b[39m3\u001b[39;49m])  \u001b[39m# 报错，不可hash\u001b[39;00m\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/apple/dsc/dsc/25.dict_set/dict_set.ipynb#W2sZmlsZQ%3D%3D?line=26'>27</a>\u001b[0m s\u001b[39m.\u001b[39madd({\u001b[39m'\u001b[39m\u001b[39ma\u001b[39m\u001b[39m'\u001b[39m:\u001b[39m1\u001b[39m})\n",
      "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "#  ！！！ set 的元素要可以hash， \n",
    "# 如果hash 不一样，则为不重复，\n",
    "# 如果hash 一样，再调用eq方法，查看值是否一样，一样则为重复\n",
    "class A():\n",
    "    pass\n",
    "    def __hash__(self) -> int:\n",
    "        return 1\n",
    "\n",
    "s = set()\n",
    "s.add(A())      # 两个A，\n",
    "s.add(A())\n",
    "\n",
    "s.add(1)\n",
    "s.add('hahh')\n",
    "s.add(b'sdf')\n",
    "s.add(True)\n",
    "s.add(complex(1,2))\n",
    "\n",
    "a = (1,2,3)     # hash 值一样，不会重复放入\n",
    "b = (1,2,3)\n",
    "s.add(a)\n",
    "s.add(b)\n",
    "\n",
    "print('set: ', s)\n",
    "\n",
    "s.add([1,2,3])  # 报错，不可hash\n",
    "s.add({'a':1})  # 报错，不可hash\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('opencv')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b04e532e6ce11d72ca7f2113c964d7a5531c18c5e6d3e97b1c42c6f9a3724305"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
